﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using common;
using System.Windows.Forms;
using Dapper;
using System.Data.SqlClient;
using DBEntity;
using DBClass;

namespace HTF_HMI
{
    public partial class AUDIT_TASK : Form
    {
        AutoSizeFormClass asc = new AutoSizeFormClass();

        public AUDIT_TASK()
        {
            InitializeComponent();             
        }

        private void AUDIT_TASK_Load(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
            BindArea();
            btn_query_Click(null, null);
        }

        private void BindArea()
        {
            var area = DBHelper.GetAuditArea();
            cmb_area.DisplayMember = "AREA_NAME";
            cmb_area.ValueMember = "AREA_ID";
            cmb_area.DataSource = area;
        }

        public void btn_query_Click(object sender, EventArgs e)
        {
            bindingSource1.DataSource = DBHelper.GetAuditTask(cmb_area.Text);            
            bindingSource2.DataSource = DBHelper.GetAuditLine();
            bindingSource3.DataSource = DBHelper.GetAuditItem();

            if (gridView1.RowCount > 0)
            {
                gridView1.SelectRows(0, 0);
            }

            repositoryItemLookUpEdit1.ValueMember = "DEVICE_ID";
            repositoryItemLookUpEdit1.DisplayMember = "DEVICE_ID";
            repositoryItemLookUpEdit1.DataSource = DBHelper.GetAuditDevice(cmb_area.Text);

            repositoryItemLookUpEdit1.PopupWidth = 500; //下拉框宽度
            repositoryItemLookUpEdit1.NullText = "";//空时的值
            repositoryItemLookUpEdit1.DropDownRows = 10;//下拉框行数
            repositoryItemLookUpEdit1.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;//要使用户可以输入，这里须设为Standard
            repositoryItemLookUpEdit1.AllowNullInput = DevExpress.Utils.DefaultBoolean.True; //可用Ctrl + Delete清空选择內容
         
        }      
        private void btn_new_Click(object sender, EventArgs e)
        {
            AUDIT_TASK_EDIT mpSheet = new AUDIT_TASK_EDIT(this, "");
            mpSheet.ShowDialog();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            int[] rows = this.gridView1.GetSelectedRows();
            if (rows.Count() == 0)
            {
                MessageBox.Show("请选择要修改的巡检任务！");
                return;
            }
            if (rows.Count() == 1)
            {
                string strId = this.gridView1.GetRowCellValue(rows[0], gridView1.Columns["TASK_ID"]).ToString();
                AUDIT_TASK_EDIT mpSheet = new AUDIT_TASK_EDIT(this, strId);
                mpSheet.ShowDialog();
            }
            if (rows.Count() > 1)
            {
                MessageBox.Show("请选择一条要修改的巡检任务！");
                return;
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {          
            int[] rows = this.gridView1.GetSelectedRows();
            if (rows.Count() == 0)
            {
                MessageBox.Show("请选择要删除的巡检任务！");
                return;
            }
            if (rows.Count() > 0)
            {
                if (MessageBox.Show("删除任务会同时删除与该任务关联的巡检点位关系，是否继续？", "确认删除？", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    List<int> ids = new List<int>();
                    foreach (int row in rows)
                    {
                        int strId = Int32.Parse(this.gridView1.GetRowCellValue(row, gridView1.Columns["TASK_ID"]).ToString());

                        ids.Add(strId);
                    }
                    bool result = DBHelper.DeleteAuditTask(ids);
                    if (result)
                    {
                        MessageBox.Show("删除成功！");
                        btn_query_Click(null, null);
                    }
                    else
                    {
                        MessageBox.Show("删除失败！");
                    }
                }
            }
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            cmb_area.SelectedIndex = 0;
            btn_query_Click(null, null);
        }

        private void btn_addline_Click(object sender, EventArgs e)
        {
            gridView3.AddNewRow();
        }
              
        private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {  
            object value = (sender as DevExpress.XtraEditors.LookUpEdit).EditValue;
            if (value != null)
            {
                for (int i = 0; i < gridView3.RowCount; i++)
                {
                    if (i != gridView3.FocusedRowHandle)
                    {
                        DBEntity.Models.AUDIT_LINE row = (DBEntity.Models.AUDIT_LINE)gridView3.GetRow(i);
                        if (row != null)
                        {
                            if (row.DEVICE_ID == value.ToString())
                            {
                                MessageBox.Show("点位编码" + row.DEVICE_ID + "已存在，请重新选择！");
                                this.gridView3.SetFocusedRowCellValue(gridView3.Columns["DEVICE_ID"], null);
                                return;
                            }
                        }
                    }
                }

                string deviceName = DBHelper.GetAuditDeviceByDeviceID(value.ToString()).FirstOrDefault().DEVICE_NAME;
                string devicePosition = DBHelper.GetAuditDeviceByDeviceID(value.ToString()).FirstOrDefault().DEVICE_TYPE_NAME;
                this.gridView3.SetFocusedRowCellValue(gridView3.Columns["DEVICE_NAME"], deviceName);
                this.gridView3.SetFocusedRowCellValue(gridView3.Columns["DEVICE_TYPE_NAME"], devicePosition);

                bindingSource3.DataSource = DBHelper.GetAuditItemByDeviceID(value.ToString());
            }
        }

        private void btn_editline_Click(object sender, EventArgs e)
        {
            int[] rows = this.gridView1.GetSelectedRows();
            if (rows.Count() == 0)
            {
                MessageBox.Show("请选择巡检任务！");
                return;
            }
            if (rows.Count() > 0)
            {
                long taskId = Int32.Parse(this.gridView1.GetRowCellValue(rows[0], gridView1.Columns["TASK_ID"]).ToString());
                string strTaskName = this.gridView1.GetRowCellValue(rows[0], gridView1.Columns["TASK_NAME"]).ToString();

                List<DBEntity.Models.AUDIT_LINE> updateLines = new List<DBEntity.Models.AUDIT_LINE>();
                List<DBEntity.Models.AUDIT_LINE> insertLines = new List<DBEntity.Models.AUDIT_LINE>();
                for (int i = 0; i < gridView3.RowCount; i++)
                {
                    DBEntity.Models.AUDIT_LINE row = (DBEntity.Models.AUDIT_LINE)gridView3.GetRow(i);
                    if (row.SEQ == 0)
                    {
                        MessageBox.Show("请填写巡检顺序！");
                        return;
                    }

                    if (row.ID==0)
                    {
                        DBEntity.Models.AUDIT_LINE insertLine = new DBEntity.Models.AUDIT_LINE();
                        insertLine = (DBEntity.Models.AUDIT_LINE)gridView3.GetRow(i);
                        insertLine.TASK_ID = taskId;
                        insertLine.TASK_NAME = strTaskName;
                        insertLines.Add(insertLine);
                    }
                    else
                    {
                      
                        DBEntity.Models.AUDIT_LINE updateLine = new DBEntity.Models.AUDIT_LINE();
                        updateLine = (DBEntity.Models.AUDIT_LINE)gridView3.GetRow(i);
                        updateLine.TASK_ID = taskId;
                        updateLine.TASK_NAME = strTaskName;
                        updateLines.Add(updateLine);
                    }
                }

                bool insertResult = true;
                bool updateResult = true;
                if (insertLines.Count > 0)
                {
                    insertResult = DBHelper.InsertAuditLine(insertLines);
                }
                if (updateLines.Count > 0)
                {
                    updateResult = DBHelper.UpdateAuditLine(updateLines);
                }
                if (insertResult && updateResult)
                {
                    MessageBox.Show("保存成功！");
                    gridView1_SelectionChanged(null, null);
                    //btn_query_Click(null, null);
                }
                else
                {
                    MessageBox.Show("保存失败！");
                }
            }
        }

        private void btn_deleteline_Click(object sender, EventArgs e)
        {
            int[] rows = this.gridView3.GetSelectedRows();
            if (rows.Count() == 0)
            {
                MessageBox.Show("请选择要删除的巡检点位！");
                return;
            }
            if (rows.Count() > 0)
            {
                List<int> ids = new List<int>();
                foreach (int row in rows)
                {
                    if (this.gridView3.GetRowCellValue(row, gridView3.Columns["ID"]) != null)
                    {
                        int strId = Int32.Parse(this.gridView3.GetRowCellValue(row, gridView3.Columns["ID"]).ToString());
                        ids.Add(strId);
                    }
                    else
                    {
                        this.gridView3.GetDataRow(row).Delete();
                    }
                }
                bool result = DBHelper.DeleteAuditLine(ids);
                if (result)
                {
                    MessageBox.Show("删除成功！");
                    gridView1_SelectionChanged(null,null);
                    //btn_query_Click(null, null);
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            int[] rows = this.gridView1.GetSelectedRows();

            if (rows.Count() == 1)
            {
                string taskId = this.gridView1.GetRowCellValue(rows[0], gridView1.Columns["TASK_ID"]).ToString();
                bindingSource2.DataSource = DBHelper.GetAuditLineByTaskID(taskId);

                if (gridView3.RowCount > 0)
                {
                    gridView3.SelectRows(0, 0);
                }
            }           
        }

        private void gridView3_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            int[] rows = this.gridView3.GetSelectedRows();

            if (rows.Count() == 1)
            {
                if (this.gridView3.GetRowCellValue(rows[0], gridView3.Columns["DEVICE_ID"]) != null)//切换左侧菜单会报错
                {
                    string deviceId = this.gridView3.GetRowCellValue(rows[0], gridView3.Columns["DEVICE_ID"]).ToString();
                    bindingSource3.DataSource = DBHelper.GetAuditItemByDeviceID(deviceId);
                }
            }
        }
    }
}
